package com.gui.ssm.service.Impl;

import com.gui.ssm.entity.User;
import com.gui.ssm.mapper.UserMapper;
import com.gui.ssm.service.IUserService;
import com.gui.ssm.service.ex.PasswordNotMatchException;
import com.gui.ssm.service.ex.UserNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(User user) throws UserNotFoundException, PasswordNotMatchException {

        User data = findUserByUsername(user.getUsername());
        if(user != null){
            if(user.getPassword().equals(data.getPassword())){
                return data;
            }else {
                throw new PasswordNotMatchException("密码错误！");
            }
        }else {
            throw new UserNotFoundException("尝试登录名不存在!");
        }
    }

    @Override
    public User add(User user) throws UserNotFoundException, PasswordNotMatchException {
        insert(user);
        return user;
    }

    private Integer insert(User user){
        Integer rows = userMapper.insert(user);
        return rows;
    }
    private User findUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }
}
